package com.ja.openchat.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.group.ChatInviteLink;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.ja.openchat.ui.base.JaBaseActivity;
import com.ja.openchat.ui.chat.ChatActivity;
import com.zy.openchat.util.DensityUtil;
import com.ja.openchat.views.UserAvatarView;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;

import org.jetbrains.annotations.NotNull;

import java.util.Map;

public class CheckChatInviteLinkDialog extends BottomPopupView {

    private ChatInviteLink mChatInviteLink;

    public CheckChatInviteLinkDialog(@NonNull @NotNull Context context,ChatInviteLink link) {
        super(context);
        mChatInviteLink = link;
    }

    @Override
    protected int getPopupWidth() {
        return (int) (XPopupUtils.getAppWidth(getContext())*0.9);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_check_chat_invite_link;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        UserAvatarView groupImage = findViewById(R.id.groupImage);
        TextView group_name = findViewById(R.id.group_name);
        TextView group_num = findViewById(R.id.group_num);
        UserAvatarView userImage1 = findViewById(R.id.userImage1);
        TextView userName1 = findViewById(R.id.userName1);
        UserAvatarView userImage2 = findViewById(R.id.userImage2);
        TextView userName2 = findViewById(R.id.userName2);
        UserAvatarView userImage3 = findViewById(R.id.userImage3);
        TextView userName3 = findViewById(R.id.userName3);
        View userLayout1 = findViewById(R.id.userLayout1);
        View userLayout2 = findViewById(R.id.userLayout2);
        View userLayout3 = findViewById(R.id.userLayout3);
        if(mChatInviteLink!=null){
            groupImage.showName(mChatInviteLink.title, DensityUtil.dp2px(35));
            group_name.setText(mChatInviteLink.title);
            group_num.setText(String.format(getContext().getString(R.string.member_count_format),mChatInviteLink.member_count));
            if(mChatInviteLink.member_user_ids!=null&&mChatInviteLink.member_user_ids.size()>0){
                if(mChatInviteLink.member_user_ids.size()==1){
                    userLayout1.setVisibility(View.VISIBLE);
                    userLayout2.setVisibility(View.GONE);
                    userLayout3.setVisibility(View.GONE);
                    showUserInfo(userImage1,userName1,mChatInviteLink.member_user_ids.get(0));
                }else if(mChatInviteLink.member_user_ids.size()==2){
                    userLayout1.setVisibility(View.VISIBLE);
                    userLayout2.setVisibility(View.VISIBLE);
                    userLayout3.setVisibility(View.GONE);
                    showUserInfo(userImage1,userName1,mChatInviteLink.member_user_ids.get(0));
                    showUserInfo(userImage2,userName2,mChatInviteLink.member_user_ids.get(1));
                }else{
                    userLayout1.setVisibility(View.VISIBLE);
                    userLayout2.setVisibility(View.VISIBLE);
                    userLayout3.setVisibility(View.VISIBLE);
                    showUserInfo(userImage1,userName1,mChatInviteLink.member_user_ids.get(0));
                    showUserInfo(userImage2,userName2,mChatInviteLink.member_user_ids.get(1));
                    showUserInfo(userImage3,userName3,mChatInviteLink.member_user_ids.get(2));
                }
            }else{
                userLayout1.setVisibility(View.GONE);
                userLayout2.setVisibility(View.GONE);
                userLayout3.setVisibility(View.GONE);
            }
        }
        findViewById(R.id.cancle_btn).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        findViewById(R.id.joinBtn).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mChatInviteLink==null){
                    return;
                }
                if(getContext() instanceof JaBaseActivity){
                    ((JaBaseActivity)getContext()).showProgress();
                }
                boolean isTransferLink = false;
                if(!TextUtils.isEmpty(mChatInviteLink.invite_link)&&(mChatInviteLink.invite_link.startsWith("https://")||mChatInviteLink.invite_link.startsWith("http://"))){
                    isTransferLink = true;
                }
                TelegramManager.getTelegramManager().joinChatByInviteLink(getContext(),mChatInviteLink.invite_link,isTransferLink,new TgResultBlock<ChatInfo>(){
                    @Override
                    public void onResult(Map<String, Object> request, JsonObject response, ChatInfo obj) {
                        if(getContext() instanceof JaBaseActivity){
                            ((JaBaseActivity)getContext()).hideProgress();
                        }
                        if(obj!=null){
                            ChatActivity.startActivity(getContext(),obj);
                        }else{
                            if(getContext() instanceof JaBaseActivity){
                                ((JaBaseActivity)getContext()).showErrorTip(getContext().getString(R.string.request_error));
                            }
                        }
                        dismiss();
                    }

                    @Override
                    public void onTimeout(Map<String, Object> request) {
                        if(getContext() instanceof JaBaseActivity){
                            ((JaBaseActivity)getContext()).hideProgress();
                            ((JaBaseActivity)getContext()).showErrorTip(getContext().getString(R.string.request_error));
                        }
                        dismiss();
                    }
                });
            }
        });
    }

    private void showUserInfo(UserAvatarView avatarView, TextView nameTextView, long userId){
        TelegramManager.getTelegramManager().requestContactInfo(getContext(), userId, new TgResultBlock<UserInfo>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, UserInfo obj) {
                if(obj!=null){
                    avatarView.showUser(obj, DensityUtil.dp2px(25));
                    nameTextView.setText(obj.displayName());
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {

            }
        });
    }

}
